<?php
namespace admin\controller;
use framework\core\Controller;
use framework\core\Factory;
use framework\tools\Upload;
use framework\tools\Image;
class TopicController extends Controller{
    public function addAction(){
        $this->smarty->display("topic/add.html");
    }
    public function addHandleAction(){
        $data['topic_title']=$_POST['topic_title'];
        $data['topic_desc']=$_POST['topic_description'];
        $model=Factory::M('TopicModel');
        $res=$model->checkData($data);
        if ($res){
            $upload=new Upload();
            $upload->setUploadPath(UPLOAD_PATH.'topic/');
            $upload_path=$upload->doUpload($_FILES['topic_thumb']);
            $image=new Image(UPLOAD_PATH.'topic/'.$upload_path);
            $image->setThumbPath();
            $thumb_path=$image->makeThumb(50,50);
            $data['topic_thumb']=$thumb_path;
            $id=$model->insert($data);
            if ($id){
                $this->jump('添加成功','index.php?m=admin&c=Topic&a=index');
            }else{
                $this->jump('添加失败','index.php?m=admin&c=Topic&a=add');
            }
        }else{
            $this->jump('数据不合法,详细信息如下'.$model->getError(),'index.php?m=admin&c=Topic&a=add');
        }
    }
    public function indexAction(){
        $model=Factory::M('TopicModel');
        $topics=$model->getAllTopics();
        $this->smarty->assign("topics",$topics);
        $this->smarty->display("topic/index.html");
    }
    //定义删除方法
    public function deleteAction(){
        $topic_id=$_GET['id'];
        $model=Factory::M('TopicModel');
        $result=$model->getInfoByid($topic_id);
        $thumb_path=$result['topic_thumb'];
        $upload_path=str_replace('thumb_','',$thumb_path);
        @unlink(UPLOAD_PATH.'topic/'.$upload_path);
        @unlink(THUMB_PATH.$thumb_path);
        $res=$model->delete($topic_id);
        if ($res){
            $this->jump('删除成功','index.php?m=admin&c=Topic&a=index');
        }else{
            $this->jump('删除失败','index.php?m=admin&c=Topic&a=index');
        }
    }
    //定义一个显示修改页面的方法
    public function updateAction(){
        $topic_id=$_GET['id'];
        $model=Factory::M('TopicModel');
        $info=$model->getInfoByid($topic_id);
        $this->smarty->assign('info',$info);
        $this->smarty->display("topic/edit.html");
    }
    //定义一个接收表单提交数据的方法
    public function updateHandleAction(){
        $topic_id=$_POST['topic_id'];
        $data['topic_title']=$_POST['topic_title'];
        $data['topic_desc']=$_POST['topic_description'];
        $model=Factory::M('TopicModel');
        $result=$model->checkData($data);
        if ($result){
            if ($_FILES['topic_thumb']['error']==0){
                //如果用户点击了上传 生成新的缩略图
                $upload=new Upload();
                $upload->setUploadPath(UPLOAD_PATH.'topic/');
                $upload_path=$upload->doUpload($_FILES['topic_thumb']);
                //生成缩略图
                $image=new Image(UPLOAD_PATH.'topic/'.$upload_path);
                $image->setThumbPath();
                $thumb_path=$image->makeThumb(50,50);
                $data['topic_thumb']=$thumb_path;
                //删除原来的上传图片缩略图
                $old_thumb_path=$_POST['old_topic_thumb'];
                $old_upload_path=str_replace('thumb_','',$old_thumb_path);
                @unlink(UPLOAD_PATH.'topic/'.$old_thumb_path);
                @unlink(THUMB_PATH.$old_thumb_path);
            }
        }else{
            $this->jump('数据不合法，错误信息是'.$model->getError(),"index.php?
            m=admin&c=Topic&a=update&id=$topic_id");
        }
        $res=$model->update($data,array('topic_id'=>$topic_id));
        if ($res){
            $this->jump('更新成功','index.php?m=admin&c=Topic&a=index');
        }else{
            $this->jump('更新失败','index.php?m=admin&c=Topic&a=update&id=$topic_id');
        }
    }
}